//
//  CLUnderViewController.m
//  Charles
//
//  Created by Leo on 2019/4/11.
//  Copyright © 2019 charles. All rights reserved.
//

#import "CLUnderViewController.h"
static const NSString *kPages = @"Pages";
static const NSString *kPageViewName = @"PageViewName";
static const NSString *kPageTitle = @"PageTitle";

@interface CLUnderViewController () <UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UITableView * tableView;
@property (strong, nonatomic) NSMutableArray *pageArray;

@end

@implementation CLUnderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
    [self setUpViews];
}

- (NSMutableArray *)pageArray {
    if (nil == _pageArray) {
        _pageArray = [[NSMutableArray alloc]initWithCapacity:0];
    }
    return _pageArray;
}

- (void) setDataArray:(NSArray *)dataArray {
    NSParameterAssert(dataArray);
    [self.pageArray addObjectsFromArray:dataArray];
    [self.tableView reloadData];
}

- (void)setUpViews {
    [self.tableView makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.equalTo(0);
    }];
}

- (UITableView *)tableView {
    return CL_LAZY(_tableView,({
        UITableView *table = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        table.dataSource = self;
        table.delegate = self;
        [self.view addSubview:table];
        table;
    }));
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45.0f;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.pageArray.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary * pageDict = self.pageArray[indexPath.row];
    
    NSString *className = [pageDict objectForKey:kPageViewName];
    NSString *pageTitle = [pageDict objectForKey:kPageTitle];
    NSParameterAssert(className);
    Class pageClass = NSClassFromString(className);
    CLBaseViewController *pushView = [[pageClass alloc]init];
    pushView.hidesBottomBarWhenPushed = YES;
    pushView.title = pageTitle;
    [self.navigationController pushViewController:pushView animated:YES];
}

- (void)pushViewController:(UIViewController *)viewController {
    [self.navigationController pushViewController:viewController animated:YES];
}

#pragma mark - UITableVeiwDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * strId = @"Id";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:strId];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strId];
    }
    if (indexPath.row < self.pageArray.count) {
        NSDictionary *pageDict = [self.pageArray objectAtIndex:indexPath.row];
        NSString * title = [pageDict objectForKey:kPageTitle];
        cell.textLabel.text = title;
    }
    return cell;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
